import java.util.Arrays;
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.lang.String;
import java.util.*;

public class ContactCollection
{
        private Contact[] collection;
        private int count;
        private String password;
        
        //객체 배열 collection 생성
        public ContactCollection()
        {
                collection = new Contact[100];
                count = 0;
                password = "0000";
        }
                
        //배열 크기 증가
        private void increaseSize()
        {
                Contact[] temp = new Contact[collection.length * 2];
                for(int i=0; i<collection.length; i++)
                        temp[i] = collection[i];
                collection = temp;
        }
        
        //Get the password
        public String getPassword()
        {
                return password;
        }
        
        //Change the password
        public void changePassword(String pw)
        {
                password = pw;
        }

        //Add a new contact
        public String addContact(String na, String pos, String dep, String com, String add, String tel, String fa, String mob, String em, String home)
        {
        		int j=1;
                if(count == collection.length)
                        increaseSize();
                for(int i=0; i<count; i++){
                    String getName = collection[i].getName();
                    if(j==1){
                    	if(getName.equals(na)){
                    		j++;
                    	}
                    }
                    else {
                    	String newstr = na + " " + j;
                    	if(getName.equals(newstr)){
                    		j++;
                    	}
                    }
                }
                if(j != 1)
                	na = na + " " +j;
                collection[count] = new Contact(na, pos, dep, com, add, tel, fa, mob, em, home);
                String str = collection[count].toString(); //추가된 연락처 출력
                count++;
                return str;
        }
        
        //Display all contacts -> 이름, 연락처만 정렬해서 출력
        public String displayContact()
        {
            String str="";
            
        	String[] temp = new String [count];
        	if(count==0)
        	{
        		System.out.println("저장된 연락처가 없습니다.");
        		str = "저장된 연락처가 없습니다.";
        	}
        	else {
            for(int i=0; i<count; i++)
            {
            	temp[i]=collection[i].getName();
            }
            
            Arrays.sort(temp);
            
            for(int i=0; i<count; i++)
            {
            	for(int j=0; j<count; j++)
            	{
            		if(temp[i].equals(collection[j].getName()))
            		{
            			System.out.println("name : " +collection[j].getName()+" mobile : "+collection[j].getMobile());
            			str += "name : " +collection[j].getName()+" mobile : "+collection[j].getMobile()+"\n";
            		}
            	}
            }
        	}

            return str;    
        }
        
        //View a contact
        public String viewContact(String name)
        {
        	boolean chk=false;
        	String str="";
    		for(int i=0; i<count; i++)
    		{
    			if(name.equals(collection[i].getName()))
    			{
    				System.out.println(collection[i]);
    				chk=true;
    				str = collection[i].toString();
    			}
    		}
    		if(chk==false)
    		{
    			System.out.println("입력하신 이름과 일치하는 연락처가 없습니다.");
    			str = "입력하신 이름과 일치하는 연락처가 없습니다.";
    		}
    		return str;
        }
   
        //Search contacts -> 검색결과 이름만 출력
        public String searchContact(String name)
        {
        		boolean chk=false;
                String result = "";
                for(int i=0; i<count; i++){
                        String getName = collection[i].getName();
                        if(getName.matches(".*" + name + ".*")){
                                result += collection[i].getName() + "\n";
                                chk=true;
                        }
                        else
                        	chk=false;
                }
                if(chk==false)
        			System.out.println("검색결과가 없습니다.");
                return result;
        }
        
        //Update contacts
        public boolean updateContact(String name, int updatenum, String updatedata)
        {
        	boolean chk = false;
                for(int i=0; i<count; i++)
                {
                        if(name.equals(collection[i].getName()))
                        {
                        	chk = true;
                                if(updatenum == 1)
                                {
                                        collection[i].setName(updatedata);
                                }
                                else if(updatenum == 2)
                                {
                                        collection[i].setPosition(updatedata);
                                }
                                else if(updatenum == 3)
                                {
                                        collection[i].setDepartment(updatedata);
                                }
                                else if(updatenum == 4)
                                {
                                        collection[i].setCompany(updatedata);
                                }
                                else if(updatenum == 5)
                                {
                                        collection[i].setAddress(updatedata);
                                }
                                else if(updatenum == 6)
                                {
                                        collection[i].setTelephone(updatedata);
                                }
                                else if(updatenum == 7)
                                {
                                        collection[i].setFax(updatedata);
                                }
                                else if(updatenum == 8)
                                {
                                        collection[i].setMobile(updatedata);
                                }
                                else if(updatenum == 9)
                                {
                                        collection[i].setEmail(updatedata);
                                }
                                else if(updatenum == 10)
                                {
                                        collection[i].setHomepage(updatedata);
                                }       
                        }
                }
                return chk;
        }
        
        //Delete a contact 
        public boolean deleteContact(String name,String check)
        {
        		boolean chk=false;
                if(check.compareTo("y") == 0)
                {
	                for(int i=0; i<count; i++)
	                        if(name.equals(collection[i].getName())){
	                                for(;i<count;i++)
	                                        collection[i] = collection[i+1];
	                                chk=true;
	                        }
	                if(chk==true){
		                count--;
		                System.out.println("삭제가 완료되었습니다!");
	                }
                }
                else if(check.compareTo("n") == 0)
                {
                  	System.out.println("삭제가 취소되었습니다.");
                }
                return chk;
        }
        
        public String toString()
        {
        	String description="";
        	
    		for(int i=0; i<count; i++)
    		{
    			description += collection[i].addString() + "\n";
    		}
            return description;
        }
        
        public String getName(int i)		
    	{
    		return collection[i].getName();
    	}
        
        public String getPosition(int i)
        {
        	return collection[i].getPosition();
        }


        public String getDepartment(int i)
        {
        	return collection[i].getDepartment();
        }

        public String getCompany(int i)
        {
        	return collection[i].getCompany();
        }

        public String getAddress(int i)
        {
        	return collection[i].getAddress();
        }


        public String getTelephone(int i)
        {
        	return collection[i].getTelephone();
        }


        public String getFax(int i)
        {
        	return collection[i].getFax();
        }

        public String getMobile(int i)
        {
        	return collection[i].getMobile();
        }


        public String getEmail(int i)
        {
        	return collection[i].getEmail();
        }

   
        public String getHomepage(int i)
        {
        	return collection[i].getHomepage();
        }
        
        
        public boolean login(String password, String password2)	
    	{
        	boolean login_chk = true;
        	if (!password.equals(password2))
    		{
				login_chk = false;
			}
			return login_chk;
    	}
        
        
        public boolean returnMenu(String menu)	
    	{
        	boolean chk = true;
			if(!(menu.equals("y") || menu.equals("n"))){
				chk = false;
			}
			return chk;
    	}
        
        
        public boolean typeCheck(String temp)
        {
        	boolean in_chk=true;
        	
		    for(int i=0;i<temp.length();i++){
		    	char c=temp.charAt(i);
		        //한글 자음, 한글 모음, 숫자 
		        if( !(( 0xAC00 <= c && c <= 0xD7A3 ) || ( 0x3131 <= c && c <= 0x318E ) || ( 0x30 <= c && c <= 0x39 )))
		        {
		        	in_chk=false;
		        	break;
		        }
		        else
		        	in_chk=true;			        
		    }
		    return in_chk;
		}
        
        
        public boolean lengthCheck(String temp, int min, int max)
        {
        	boolean in_chk=true;

		    if(temp.length() <= min || temp.length() >= max)
		    	in_chk=false;
        	
		    return in_chk;
        }
        
        
        public boolean numberCheck(String temp)
        {
        	boolean in_chk=true;
        	
		    for(int i=0;i<temp.length();i++){
		    	char c=temp.charAt(i);
		        if( !( 0x30 <= c && c <= 0x39 ))
		        {
		        	in_chk=false;
		        	break;
		        }
		        else
		        	in_chk=true;			        
		    }
		    return in_chk;
        }
        
        
        public boolean koreanCheck(String temp)
        {
        	boolean in_chk=true;
        	
		    for(int i=0;i<temp.length();i++){
		    	char c=temp.charAt(i);
		        //한글 자음, 한글 모음, 숫자 
		        if( !(( 0xAC00 <= c && c <= 0xD7A3 ) || ( 0x3131 <= c && c <= 0x318E )))
		        {
		        	in_chk=false;
		        	break;
		        }
		        else
		        	in_chk=true;			        
		    }
		    return in_chk;
		}
        
        
        public void addrFileSave(Object obj)
        {
        	try{
				String file = "content.txt";		

				FileWriter fw = new FileWriter (file);
				BufferedWriter bw = new BufferedWriter (fw);
				PrintWriter outFile = new PrintWriter (bw);

				outFile.print (obj.toString());
				outFile.println ();

				outFile.close();

			}catch(Exception e){}
        }
        
        
        public void addrFileOpen(Object obj)
        {
	        try{
				FileReader fr = new FileReader("content.txt");
				BufferedReader br = new BufferedReader(fr);
				boolean fileend = false;
				
				String data[] = new String[10];
				while(true){
					for(int i=0; i<10; i++){
						data[i] = br.readLine();
						if(data[i] == null)
						{
							fileend = true;
							break;
						}
					}
					if(fileend) break;
					this.addContact(data[0],data[1],data[2],data[3],data[4],data[5],data[6],data[7],data[8],data[9]);
				}
				br.close();
				fr.close();
				
				}catch(Exception e){}
        }
        
        
        public void pwFileSave(Object obj, String temp)
        {
	        try{
				String file = "password.txt";		
	
				FileWriter fw = new FileWriter (file);
				BufferedWriter bw = new BufferedWriter (fw);
				PrintWriter outFile = new PrintWriter (bw);
	
				outFile.print (temp);
				outFile.println ();
	
				outFile.close();
	
			}catch(Exception e){}
        }
        
        
        public void pwFileOpen(Object obj)
        {
	        try{
				FileReader fr = new FileReader("password.txt");
				BufferedReader br = new BufferedReader(fr);
		
				String cpw;
				cpw = br.readLine();
				this.changePassword(cpw);
							
				br.close();
				fr.close();
				
				}catch(Exception e){}
        }
}

